<?php namespace Install\Controller;

use Hdphp\Controller\Controller;
use Pdo;
//测试控制器
class IndexController extends Controller{

	//构造函数
	public function __init()
	{

	}

    //动作
    public function index()
    {
       View::make();
    }

    public function permit()
        {
           View::make();
        }

    public function environment()
    {

       View::make();
    }

    public function database()
    {
        if (IS_POST)
        {
            if (empty($_POST['ADMIN']) || !preg_match('/^\w+$/i', $_POST['ADMIN'])) {
                $this->error('管理员帐号错误');
            }
            if (empty($_POST['PASSWORD'])) {
                $this->error('管理员密码不能为空');
            }
            if ($_POST['PASSWORD'] !== $_POST['C_PASSWORD']) {
                $this->error('两次密码不一致');
            }
            if (empty($_POST['WEBNAME'])) {
                $this->error('网站名称不能为空');
            }
            if (empty($_POST['EMAIL'])) {
                $this->error('站长邮箱不能为空');
            }
            //================================= 连接服务器 =================================
            //
            $dsn = "mysql:host=".$_POST['DB_HOST'];
            $username=$_POST['DB_USER'];
            $password = $_POST['DB_PASSWORD'];
            try{
                $pdo = new Pdo($dsn,$username,$password);
            }catch(PDOException $e) {
                die($e->getMessage());
            }
            // echo "678";
            if (!$pdo->exec("CREATE DATABASE IF NOT EXISTS " . $_POST['DB_DATABASE'] . " CHARSET UTF8"))
            {
                // echo "string";exit;
                $this->error('创建数据库失败');
            }

            // echo MODULE_PATH . "/Install/structure.php";
            require MODULE_PATH . "/Install/structure.php";
            foreach (glob(MODULE_PATH . "/Install/*") as $f)
            {
                $info = pathinfo($f);

                if (preg_match('@\w+_1.php@', $info['basename'])) {
                    echo "<pre>";
                print_r($info);
                print_r($info['dirname'].'/'.$info['basename']);
                    require $info['dirname'].'/'.$info['basename'];
                }

            }

            exit;

            $this->success('创建数据表成功');

        }
        else
        {
            View::make();
        }

    }

    public function produce()
    {
       View::make();
    }

    public function finish()
    {
       View::make();
    }

}
